<template>
  <view>
    <view class="img">
	  <uni-icons type="back" size="30" v-if="backshow" @click="toback()"></uni-icons>
      <image src="https://syh-2.oss-cn-shanghai.aliyuncs.com/logo.png"></image>
    </view>
    <view v-show="xuanze">
      <view class="content">请选择登录身份</view>
      <view class="middle">
        <view :class="navIndex == 0 ? 'active' : ''" @click="checkIndex(0)"
          >司机</view
        >
        <view :class="navIndex == 1 ? 'active' : ''" @click="checkIndex(1)"
          >货主</view
        >
        <!-- <view :class="navIndex == 2 ? 'active' : ''" @click="checkIndex(2)"
          >专线</view
        > -->
      </view>
      <view class="footer" @click="tijiao()">确认提交</view>
    </view>
    <!-- 货主 -->
    <view v-show="huozhu" class="section">
      <view class="section_xx">基本信息填写</view>
      <view class="section_hz">
        <text>登录身份</text>
        <text>货主</text>
      </view>
      <view class="section_key">
        <view>
          <text>姓名</text>
          <input type="text" v-model="hxinmin" placeholder="请输入姓名" />
        </view>
        <view>
          <text>身份证</text>
          <input
            type="text"
            v-model="hshfeizh"
            placeholder="请输入身份证号码"
          />
        </view>
        <view>
          <text>手机号</text>
          <input type="text" v-model="hshoujih" placeholder="请输入手机号码" />
        </view>
      </view>
      <view class="section_box">
        <view class="section_fl" @click="ShiperReg()">确认提交</view>
        <!--  <view class="section_fr" @click="toskip()">跳过</view> -->
      </view>
    </view>
    <!-- 司机 -->
    <view v-show="siji" class="section">
      <view class="section_xx">基本信息填写</view>
      <view class="section_hz">
        <text>登录身份</text>
        <text>司机</text>
      </view>
      <view class="section_key">
        <view>
          <text>姓名</text>
          <input type="text" v-model="sxinmin" placeholder="请输入姓名" />
        </view>
        <view>
          <text>身份证</text>
          <input
            type="text"
            v-model="sshfeizh"
            placeholder="请输入身份证号码"
          />
        </view>
        <view>
          <text>手机号</text>
          <input type="text" v-model="sshoujih" placeholder="请输入手机号码" />
        </view>
        <view>
          <text>车牌号</text>
          <car-number-input
            @numberInputResult="numberInputResult"
            :defaultStr="defaultNum"
          >
          </car-number-input>
        </view>
      </view>
      <view class="section_box">
        <view class="section_fl" @click="DriverReg()">确认提交</view>
        <!-- <view class="section_fr" @click="skip()">跳过</view> -->
      </view>
    </view>
  </view>
</template>

<script>
import {
  GetWeiXinMobileLogin,
  ShiperReg,
  DriverReg,
  GetUserById,
} from "@/public/Api/update.js";
import { setValue, getValue } from "@/public/storeage/index.js";
export default {
  data() {
    return {
      navIndex: "4",
      xuanze: true,
      huozhu: false,
      siji: false,
      roles: "",
      backshow:false,
      hxinmin: "",
      hshfeizh: "",
      hshoujih: "",

      sxinmin: "",
      sshfeizh: "",
      sshoujih: "",

      defaultNum: "",
    };
  },
  onLoad() {
    this.Authenticate();
    //this.login();
    this.GetUserById();
  },
  methods: {
    checkIndex(e) {
      this.navIndex = e;
      console.log(this.navIndex);
    },
    tijiao() {
      var navIn = this.navIndex;
	  this.backshow=true
      if (navIn == "0") {
        this.xuanze = false;
        this.siji = true;
      } else if (navIn == "1") {
        this.xuanze = false;
        this.huozhu = true;
      }
      console.log(navIn);
    },
    //选择返回
	toback(){
		this.xuanze = true;
		this.siji = false;
		this.huozhu = false;
		this.backshow=false
	},
    numberInputResult(e) {
      this.defaultNum = e;
    },

    skip() {
      uni.reLaunch({
        url: "/pages/index/sjindex",
      });
    },
    toskip() {
      uni.reLaunch({
        url: "/pages/index/index",
      });
    },

    // 获取code
    async login() {
      var that = this;
      uni.login({
        provider: "weixin",
        success: function (loginRes) {
          console.log(loginRes);
          that.GetWeiXinMobileLogin(loginRes);
        },
      });
    },

    // 应用授权
    async Authenticate() {
      try {
        const { data: res } = await Authenticate({
          userNameOrEmailAddress: "admin",
          password: "123qwe",
        });
      } catch (e) {}
    },
    async GetUserById() {
      var id = getValue("id");
      try {
        const { data: res } = await GetUserById({
          id,
        });
        console.log(res, "res123");
        this.sshoujih = res.result.mobile;
        this.hshoujih = res.result.mobile;
      } catch (e) {}
    },

    // 微信授权登录
    async GetWeiXinMobileLogin(loginRes) {
      console.log(loginRes, "123456");
      var code = loginRes.code;
      try {
        const { data: res } = await GetWeiXinMobileLogin({
          code,
        });
        // setValue("id", res.result.id);
        // setValue("code", code);
        // setValue("approved", res.result.approved);
        this.roles = res.result.roles;
      } catch (e) {}
    },

    // 货主注册
    async ShiperReg() {
      var roles = this.navIndex;
      var id = getValue("id");
      var identityNumber = this.hshfeizh;
      var trueName = this.hxinmin;
      var mobile = this.hshoujih;
      if (trueName == "") {
        uni.showToast({
          title: "姓名不能为空",
          icon: "none",
          duration: 2000,
        });
        return false;
      }
      if (identityNumber == "") {
        uni.showToast({
          title: "身份证不能为空",
          icon: "none",
          duration: 2000,
        });
        return false;
      }
      if (mobile == "") {
        uni.showToast({
          title: "手机号不能为空",
          icon: "none",
          duration: 2000,
        });
        return false;
      }
      try {
        const { data: res } = await ShiperReg({
          roles,
          id,
          identityNumber,
          trueName,
          mobile,
        });
        if (res.result.message == "ok") {
          uni.showToast({
            title: `${res.result.message}`,
            icon: "success",
          });
          setValue("status", res.result.data.status);
          setValue("roles", res.result.data.roles);
          setValue("approved", res.result.data.approved);
          setTimeout(function () {
            uni.reLaunch({
              url: "/pages/index/index",
            });
          }, 600);
        } else {
          uni.showToast({
            title: `${res.result.message}`,
            icon: "none",
          });
        }
      } catch (e) {}
    },

    // 司机注册
    async DriverReg() {
      var roles = this.navIndex;
      var id = getValue("id");
      var identityNumber = this.sshfeizh;
      var trueName = this.sxinmin;
      var mobile = this.sshoujih;
      var carNumber = this.defaultNum;

      if (trueName == "") {
        uni.showToast({
          title: "姓名不能为空",
          icon: "none",
          duration: 2000,
        });
        return false;
      }
      if (identityNumber == "") {
        uni.showToast({
          title: "身份证不能为空",
          icon: "none",
          duration: 2000,
        });
        return false;
      }
      if (mobile == "") {
        uni.showToast({
          title: "手机号不能为空",
          icon: "none",
          duration: 2000,
        });
        return false;
      }
      if (carNumber == "") {
        uni.showToast({
          title: "车牌号不能为空",
          icon: "none",
          duration: 2000,
        });
        return false;
      }
      try {
        const { data: res } = await DriverReg({
          roles,
          id,
          identityNumber,
          trueName,
          mobile,
          carNumber,
        });
        if (res.result.message == "ok") {
          uni.showToast({
            title: `${res.result.message}`,
            icon: "success",
          });
		  this.backshow=false;
          setValue("status", res.result.data.status);
          setValue("roles", res.result.data.roles);
          setValue("approved", res.result.data.approved);
          setTimeout(function () {
            uni.reLaunch({
              url: "/pages/index/sjindex",
            });
          }, 600);
        } else {
          uni.showToast({
            title: `${res.result.message}`,
            icon: "none",
          });
        }
      } catch (e) {}
    },
  },
};
</script>

<style lang="scss" scoped>
.img {
  padding-top: 117upx;
  uni-icons{
	  margin-top: -10upx;
	  margin-left: 10upx;
  }
  image {
    display: block;
    width: 114upx;
    height: 117upx;
    margin: 0 auto;
  }
}

.content {
  font-size: 38upx;
  color: #000;
  text-align: center;
  font-weight: bold;
  padding-top: 120upx;
}

.middle {
  width: 200upx;
  margin: 0 auto;

  view {
    font-size: 38upx;
    color: #000;
    text-align: center;
    font-weight: bold;
    padding-top: 100upx;
    padding-bottom: 16upx;
    border-bottom: 1upx solid #e5e5e5;
  }

  .active {
    color: #f04d3e;
  }
}

.footer {
  font-size: 38.55upx;
  font-weight: 500;
  color: rgba(255, 255, 255, 1);
  width: 436upx;
  height: 82upx;
  opacity: 1;
  border-radius: 17.52upx;
  background: #f04d3e;
  text-align: center;
  line-height: 82upx;
  margin: 0 auto;
  position: fixed;
  left: 156upx;
  bottom: 115upx;
}

.section {
  .section_xx {
    font-size: 38upx;
    font-weight: bold;
    color: #000;
    padding-top: 80upx;
    padding-left: 53upx;
  }

  .section_hz {
    display: flex;
    align-items: center;
    padding-top: 65upx;
    width: 722upx;
    margin: 0 auto;
    border-bottom: 2upx solid rgba(229, 229, 229, 1);
    padding-bottom: 35upx;

    text {
      font-size: 38upx;
      font-weight: bold;
      color: #000;
      padding: 0 200upx 0 40upx;
    }
  }

  .section_key {
    padding: 15upx 43upx 0;

    view {
      width: 100%;
      height: 71.85upx;
      opacity: 1;
      border-radius: 10.51upx;
      background: rgba(247, 245, 245, 1);
      margin-top: 25upx;
      display: flex;
      align-items: center;

      text {
        width: 190upx;
        font-size: 31.54upx;
        font-weight: bold;
        padding-left: 15upx;
      }

      input {
        font-size: 24.53upx;
      }
    }
  }

  .section_box {
    position: fixed;
    left: 156upx;
    bottom: 115upx;

    .section_fl {
      font-size: 38.55upx;
      font-weight: 500;
      color: rgba(255, 255, 255, 1);
      width: 436upx;
      height: 82upx;
      opacity: 1;
      border-radius: 17.52upx;
      background: #f04d3e;
      text-align: center;
      line-height: 82upx;
      margin: 0 auto;
    }

    .section_fr {
      font-size: 31.54upx;
      color: rgba(239, 77, 61, 1);
      text-align: center;
      padding-top: 33upx;
    }
  }
}
</style>
